<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Controller;

use Think\Controller;
use Home\Controller\BaseController;
/**
 * Description of GoodsController
 *
 * @author Administrator
 */
class GoodsController extends BaseController
{

    public function index()
    {
        if (IS_POST) {
            $p = I('post.');
            $user_id = trim(session('user_id'));
            if(empty($user_id)){
                $this->error('请登录后在提问！');
                exit;
            }
            if(!trim($p['goods_id'])){
                $this->error('信息不合法！');
                exit;
            }
            if(!trim($p['goods_name'])){
                $this->error('信息不合法！');
                exit;
            }
            if(preg_match("/(select)(insert)(delete)(truncate)(update)[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",trim($p['question']))){
                $this->error('信息不合法！');
                exit;
            }
            //判断该用户10分钟内是否重复提问该商品
            $where['goods_id'] = $p['goods_id'];
            $where['user_id'] = session('user_id');
            $addtime = M('advice')->where($where)->order('addtime desc')->field('addtime')->find();
            if (($addtime['addtime'] + 600) < time()) {
                $data['addtime'] = time();
                $data['user_id'] = session('user_id');
                $data['user_name'] = session('user_auth')['user_name'];
                $data['question'] = trim($p['question']);
                $data['goods_id'] = trim($p['goods_id']);
                $data['goods_name'] = trim($p['goods_name']);

                $data['is_show'] = 0;
                M('advice')->add($data);
                $this->success('提问成功，时长来看看管理员的解答吧!');
            } else {
                $s = date('i:s', $addtime['addtime'] + 600 - time());
                $this->error($s . '分钟后可再次提问');
            }
        }

        $goods_id = 'goods_id=' . I('get.id');

        $Mgood = M("goods");      //商品表
        $goods_attr = M("goods_attri_info");      //属性表
        $goods_para = M("goods_para_info");      //规格表
        $goods_browse = M('goods_browse');          //商品浏览历史表

        $goods = $Mgood->where($goods_id)->find();
        $goods['images'] = unserialize($goods['goods_images']);

        $num = M('cart')->where(array('buyer_id' => session('user_id')))->count();

        $goods_comment = M('goods_comment');  //评价表
        $w['goods_id'] = I('get.id');
        $w['is_show'] = 1;
        $res = $goods_comment->where($w)->order('add_time desc')->select();
//        p($w);die;

        $branch = array();
        $i = 0;
        foreach ($res as $k => $v) {
            ++$i;
            $branch['miaoshu'] += $v['miaoshu'];
            $branch['fuwu'] += $v['fuwu'];
            $branch['wuliu'] += $v['wuliu'];
            $res[$k]['xing'] = ceil(($v['miaoshu'] + $v['fuwu'] + $v['fahuo'] + $v['wuliu']) / 4);
        }
        $branch['miaoshu'] = sprintf("%.1f", $branch['miaoshu'] / $i);
        $branch['fuwu'] = sprintf("%.1f", $branch['fuwu'] / $i);
        $branch['wuliu'] = sprintf("%.1f", $branch['wuliu'] / $i);
        $this->assign('branch', $branch);

        $where3['store_id'] = $goods['store_id'];
        $where3['goods_sale'] = 1;
        $where3['goods_state'] = 1;
        $datawww = $Mgood->where($where3)->order('goods_hits desc')->limit(6)->select();
        $attr = $goods_attr->field('model,stock,price,mktprice')->where($goods_id)->select();
        foreach ($attr as $key => $value) {
            $json[$value['model']] = $value;
        }

        //商品的规格
        $jsonAttr = array(
            "mktprice" => $goods['goods_markprice'],
            "price" => $goods['goods_price'],
            "stock" => (int)$goods['goods_stock'],
            "sys_attrprice" => $json,
        );
        $jsonAttr = json_encode($jsonAttr);
        $this->assign("jsonAttr", $jsonAttr);

        //商品属性前台显示列表
        $attri = M('goodsAttriClass')->where($goods_id)->group('depth')->select();
        foreach ($attri as $key => $value) {
            $where['goods_id'] = I('get.id');
            $where['depth'] = $value['depth'];
            $attri[$key]['guige'] = $value['model'];
            $attri[$key]['class'] = M('goodsAttriClass')->where($where)->select();
        }
        $this->assign("attri", $attri);

        //商品的参数
        $spec = $goods_para->field('paraname,value')->where($goods_id)->select();
        $this->assign("spec", $spec);

        //分类列表
        $class1 = M('category')->where('parent_id=0')->select();
        $class2 = M('category')->where('parent_id=' . $goods['goods_class1'])->select();
        $class3 = M('category')->where('parent_id=' . $goods['goods_class2'])->select();
        $class1_name1 = M('category')->where('cat_id=' . $goods['goods_class1'])->getField('cat_name');
        $class1_name2 = M('category')->where('cat_id=' . $goods['goods_class2'])->getField('cat_name');
        $class1_name3 = M('category')->where('cat_id=' . $goods['goods_class3'])->getField('cat_name');
        $this->assign("class1_name1", $class1_name1);
        $this->assign("class1_name2", $class1_name2);
        $this->assign("class1_name3", $class1_name3);
        $this->assign("class1", $class1);
        $this->assign("class2", $class2);
        $this->assign("class3", $class3);


        //本店铺最新上架的商品(如果商家没有上传新的产品，自动获取平台最新的产品)
        $where['store_id'] = $goods['store_id'];
        $where['goods_sale'] = 1;
        $newGoods = M('goods')->where($where)->order('goods_created desc')->limit(6)->select();
        if (empty($newGoods)) {
            $newGoods = M('goods')->order('goods_created desc')->limit(6)->select();
        }
        $this->assign("newGoods", $newGoods);

        //获取商品同类品牌的商品
        $where1['goods_class1'] = $goods['goods_class1'];
        $where1['goods_sale'] = 1;
        $classGoods = M('goods')->where($where1)->order('goods_created desc')->limit(6)->select();
        $this->assign("classGoods", $classGoods);

        //商家信息
        $storeWhere['store_id'] = $goods['store_id'];
        $store = M('store')->where($storeWhere)->find();
        $this->assign('store', $store);

        //浏览次数+1
        M('goods')->where($goods_id)->setInc('goods_hits', 1);

        //记录用户浏览商品信息
        if (session('user_id')) {
            $arr["goods_id"] = I('get.id');
            $arr["member_id"] = session('user_id');
            $re = $goods_browse->where($arr)->select();
            if ($re) {
                $data['browsetime'] = time();
                $goods_browse->where($arr)->data($data)->save();
            } else {
                $data['goods_id'] = $arr["goods_id"];
                $data['member_id'] = $arr["member_id"];
                $data['browsetime'] = time();
                $data['gc_id'] = $goods['goods_class'];
                $data['gc_id_1'] = $goods['goods_class1'];
                $data['gc_id_2'] = $goods['goods_class2'];
                $data['gc_id_3'] = $goods['goods_class3'];
                $goods_browse->data($data)->add();
            }
        }

        //商品被收藏数量
        $goods['num'] = M('favorites')->where(array('fav_id' => I('get.id'), 'fav_type' => 20))->count();

        //登陆判断是否收藏
        if (session('user_id')) {
            $fa['member_id'] = session('user_id');
            $fa['fav_id'] = $storeWhere['store_id'];
            $fa['fav_type'] = 40;
            $user_num = M('Favorites')->where($fa)->count();
            $w['member_id'] = $fa['member_id'];
            $w['fav_id'] = I('get.id');
            $w['fav_type'] = 20;
            $goods_fa = M('Favorites')->where($w)->count();
            $this->assign('user_num', $user_num);
            $this->assign('goods_fa', $goods_fa);
        }

        //提问解答
        $advic = M('advice'); // 实例化User对象
        $advic_where['goods_id'] = I('get.id');
        $advic_where['is_show'] = 1;
        $count = $advic->where($advic_where)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $advic->where($advic_where)->order('addtime desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('advice', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出
        $advic_num = $advic->where($advic_where)->count();
        $this->assign('advic_num', $advic_num);// 赋值数据集

        $this->assign("datawww", $datawww);
        $this->assign("res", $res);
        $this->assign("num", $num);
        $this->assign("goods", $goods);
        $this->assign("attr", $attr);

        $this->display();
    }

    //用户评论信息
    public function goodsComment()
    {
        $goods_id = I('goods_id');
        $map['goods_id'] = $goods_id;
        $map['is_show'] = 1;
        $count = M('goodsComment')->where($map)->count();    //计算总数
        $Page = new \Think\PageAjax($count, 15);
        $lists = M('goodsComment')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('id DESC')->select();
        $data['count'] = $count;


        //用户若登陆 判断是否有点赞
        if (session('user_id')) {
            $wh['user_id'] = session('user_id');
            $wh['goods_id'] = $goods_id;
            $zan = M('goods_comment_zan')->where($wh)->field('comment_id')->select();
            foreach ($zan as $v) {
                $comment_zan[] = $v['comment_id'];
            }
        }


        if (empty($lists)) {
            $data['html'] .= '<div class="no-comment loading">暂时还没有任何评论！</div>';
        } else {
            foreach ($lists as $key => $value) {
                if ($comment_zan) {
                    in_array($value['id'], $comment_zan) ? $in = " on" : $in = null;
                }
                $nn = '匿名评价';
                $data['html'] .= '<li class="am-comment">';
                $data['html'] .= '<a href=""><img src="' . $value['headimgurl'] . '" height="100" width="100"></a>';
                $data['html'] .= '<div class="am-comment-main">';
                $data['html'] .= '<header class="am-comment-hd">';
                $data['html'] .= '<div class="am-comment-meta">';
                if($value['is_anonymous']){
                    $data['html'] .= '<a href="#link-to-user" class="am-comment-author">'.$value['user_name']. '</a>';
                }else{
                    $data['html'] .= '<p>匿名评价</p>';
                }
                $data['html'] .= '<time datetime="">' . date('Y-m-d', $value['add_time']) . '</time>';
                $data['html'] .= '</div>';
                $data['html'] .= '</header>';
                $data['html'] .= '<div class="am-comment-bd">';
                $data['html'] .= '<div class="tb-rev-item " data-id="255776406962">';
                $data['html'] .= '<div class="J_TbcRate_ReviewContent tb-tbcr-content ">';
                $data['html'] .= $value['comment'];
                $data['html'] .= '</div>';
                $data['html'] .= '<div class="tb-r-act-bar">';
                $data['html'] .= '</div></div></div></div></li>';
            }
        }
        return $this->ajaxReturn($data);
    }


}